package com.robot.member.service;

import java.time.LocalDateTime;
import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.robot.member.dao.FeedbackDao;
import com.robot.member.pojo.Feedback;

/**
 * feedback服务层
 *
 * @author
 */
@Service
public class FeedbackService {
    @Autowired
    private FeedbackDao feedbackDao;

    @Autowired
    private IdWorker idWorker;

    /**
     * 查询全部列表
     *
     * @return
     */
    public List<Feedback> findAll() {
        return feedbackDao.findAll();
    }


    /**
     * 条件查询+分页
     *
     * @param whereMap
     * @param page
     * @param size
     * @return
     */
    public Page<Feedback> findSearch(Map whereMap, int page, int size) {
        Specification<Feedback> specification = createSpecification(whereMap);
        PageRequest pageRequest = PageRequest.of(page - 1, size);
        return feedbackDao.findAll(specification, pageRequest);
    }


    /**
     * 条件查询
     *
     * @param whereMap
     * @return
     */
    public List<Feedback> findSearch(Map whereMap) {
        Specification<Feedback> specification = createSpecification(whereMap);
        return feedbackDao.findAll(specification);
    }

    /**
     * 根据ID查询实体
     *
     * @param id
     * @return
     */
    public Feedback findById(String id) {
        return feedbackDao.findById(id).orElse(null);
    }

    /**
     * 增加
     *
     * @param feedback
     */
    public void add(Feedback feedback) {
        //雪花分布式ID生成器
        feedback.setId(idWorker.nextId() + "");
        feedback.setStatus("0");
        LocalDateTime now = LocalDateTime.now();
        feedback.setCreatedAt(now);
        feedback.setUpdatedAt(now);
        feedbackDao.save(feedback);
    }

    /**
     * 修改
     *
     * @param feedback
     */
    public void update(Feedback feedback) {
        feedbackDao.save(feedback);
    }

    /**
     * 删除
     *
     * @param id
     */
    public void deleteById(String id) {
        feedbackDao.deleteById(id);
    }

    /**
     * 动态条件构建
     *
     * @param searchMap
     * @return
     */
    private Specification<Feedback> createSpecification(Map searchMap) {
        return new Specification<Feedback>() {
            @Override
            public Predicate toPredicate(Root<Feedback> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                String id = "id";
                if (searchMap.get(id) != null && !"".equals(searchMap.get(id))) {
                    predicateList.add(cb.like(root.get(id).as(String.class), "%" + (String) searchMap.get(id) + "%"));
                }
                // 会员id
                String memberId = "memberId";
                if (searchMap.get(memberId) != null && !"".equals(searchMap.get(memberId))) {
                    predicateList.add(cb.like(root.get(memberId).as(String.class), "%" + (String) searchMap.get(memberId) + "%"));
                }
                // 会员id
                String openId = "openId";
                if (searchMap.get(openId) != null && !"".equals(searchMap.get(openId))) {
                    predicateList.add(cb.like(root.get(openId).as(String.class), "%" + (String) searchMap.get(openId) + "%"));
                }
                // 反馈的内容
                String content = "content";
                if (searchMap.get(content) != null && !"".equals(searchMap.get(content))) {
                    predicateList.add(cb.like(root.get(content).as(String.class), "%" + (String) searchMap.get(content) + "%"));
                }
                // 联系电话
                String phone = "phone";
                if (searchMap.get(phone) != null && !"".equals(searchMap.get(phone))) {
                    predicateList.add(cb.like(root.get(phone).as(String.class), "%" + (String) searchMap.get(phone) + "%"));
                }
                //剔除软删除数据
                String deletedAt = "deletedAt";
                predicateList.add(cb.equal(root.get(deletedAt), cb.nullLiteral(LocalDateTime.class)));
                //创建时间倒序
                String createdAt = "createdAt";
                query.orderBy(cb.desc(root.get(createdAt)));
                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * 根据memberId查会员的所有反馈并分页
     *
     * @param memberId
     * @param page
     * @param size
     * @return
     */
    public Page<Feedback> findAllByMemberIdOrderByCreatedAt(String memberId, int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return feedbackDao.findAllByMemberIdOrderByCreatedAt(memberId, pageable);
    }

    /**
     * 所有反馈并分页
     *
     * @return
     */
    public Page<Feedback> findAllByDeletedAtNullOrderByCreatedAt(int page, int size) {
        Pageable pageable = PageRequest.of(page - 1, size);
        return feedbackDao.findAllByDeletedAtNullOrderByCreatedAt(pageable);
    }

    /**
     * 根据id和memberId查会员的某一反馈详情
     *
     * @param id
     * @param memberId
     * @return
     */
    public Feedback findByIdAndMemberId(String id, String memberId) {
        return feedbackDao.findByIdAndMemberId(id, memberId);
    }

    /**
     * 软删除
     *
     * @param id
     */
    public void updateFeedbackById(String id) {
        feedbackDao.updateFeedbackById(id);
    }

}
